// pages/register/index.js
let app = getApp()
// 获取云数据库引用
const db = wx.cloud.database()
const admin = db.collection('adminlist')

let username = ''
let password = ''
let password2 = ''
let turename = ''
let phone = ''
let phoneInt = ''
let id = ''

Page({
  data: {
  },
  // 输入用户名
  inputUname: function (event) {
    // console.log(event)
    username = event.detail
    // console.log(username)
  },
  // 输入密码
  inputPassword (event) {
    password = event.detail
    // console.log(password)
  },
  // 确认密码
  inputPassword2 (event) {
    password2 = event.detail
    // console.log(password2)
  },
  // 输入用户名
  inputName: function (event) {
    turename = event.detail
    // console.log(turename)
  },
  // 输入手机号
  inputPhoneNum (event) {
    phone = event.detail
    phoneInt = parseInt(event.detail)
    // console.log(phone.length);
    console.log(phoneInt)
    console.log(typeof (phone))
  },
  // 输入学号
  inputID (event) {
    id = event.detail
    // console.log(id)
  },

  // 注册
  register () {
    let that = this
    let flag1 = false // 学号是否存在 true为存在
    let flag2 = false // 用户名是否存在 true为存在
    // 查询用户是否已经注册
    admin.get({
      success: (res) => {
        let admins = res.data // 获取到的对象数组数据
        //  console.log(admins);
        for (let i = 0; i < admins.length; i++) { // 遍历数据库对象集合
          if (id === admins[i].id) { // 学号存在
            flag1 = true
            break
          }
        }
        for (let i = 0; i < admins.length; i++) { // 遍历数据库对象集合
          if (username === admins[i].username) { // 用户名存在
            flag2 = true
            break
          }
        }
        if (username === '') {
          wx.showToast({
            title: '用户名勿为空',
            icon: 'error',
            duration: 1500
          })
        } else if (flag2 === true) { // 已注册
          wx.showToast({
            title: '用户名已存在',
            icon: 'error',
            duration: 1500
          })
        } else if (password === '') {
          wx.showToast({
            title: '密码勿为空',
            icon: 'error',
            duration: 1500
          })
        } else if (password2 === '') {
          wx.showToast({
            title: '再次输入密码',
            icon: 'error',
            duration: 1500
          })
        } else if (password2 !== password) {
          wx.showToast({
            title: '密码有误',
            icon: 'error',
            duration: 1500
          })
        } else if (turename === '') {
          wx.showToast({
            title: '姓名勿为空',
            icon: 'error',
            duration: 1500
          })
        } else if (phone === '') {
          wx.showToast({
            title: '手机号勿为空',
            icon: 'error',
            duration: 1500
          })
        } else if (phone.length !== 11 || isNaN(phoneInt)) {
          wx.showToast({
            title: '手机号格式×',
            icon: 'error',
            duration: 1500
          })
        } else if (id === '') {
          wx.showToast({
            title: '学号勿空！',
            icon: 'error',
            duration: 1500
          })
        } else if (flag1) {
          wx.showToast({
            title: '该学号已注册',
            icon: 'error',
            duration: 1500
          })
        } else { // 未注册
          that.saveuserinfo()
        }
      }
    })
  },

  // 注册用户信息
  saveuserinfo () {
    // let that = this
    admin.add({ // 添加数据
      data: {
        username: username,
        password: password,
        turename: turename,
        collect_ph: [],
        collect_st: [],
        phone: phone,
        id: id,
        score: 0,
        signUp: []
      }
    }).then((res) => {
      console.log('注册成功！')
      wx.redirectTo({
        url: '/pages/login/login'
      })
      wx.showToast({
        title: '注册成功！',
        icon: 'success',
        duration: 3000
      })
    })
  }
})